package com.cash.loan.modular.cash.controller;

import com.cash.loan.core.upload.QiniuService;
import com.cash.loan.modular.cash.model.CashVersion;
import com.cash.loan.modular.cash.result.PageResult;
import com.cash.loan.modular.cash.result.ServerResponse;
import com.cash.loan.modular.cash.service.CashVersionService;
import com.cash.loan.modular.cash.transfer.CashVersionDto;
import com.cash.loan.modular.cash.transfer.UpdateCashVersionDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 * @Date 2018-10-08 11:09
 */
@Controller
@RequestMapping(value = "/cash/version")
@Api(description = "版本管理接口")
public class VersionController {

    private static final Logger log = LoggerFactory.getLogger(VersionController.class);

    private static final String APK_STRING = ".apk";

    private static final String DEFAULT_APK_NAME = "com.loanmarket";

    private static final String PREFIX = "/cash/version";

    @Autowired
    private QiniuService qiniuService;

    @Autowired
    private CashVersionService cashVersionService;

    @RequestMapping(value = "/listPage")
    public String listPage() {
        return PREFIX + "/version_list.html";
    }

    @GetMapping(value = "/list")
    @ResponseBody
    @ApiOperation("版本列表")
    public ServerResponse list(@RequestParam(value = "pageNo",required = false,defaultValue = "1") Integer pageNo,
                               @RequestParam(value = "pageSize",required = false,defaultValue = "20") Integer pageSize) {
        PageResult<CashVersion> result = cashVersionService.list(pageNo,pageSize);
        return ServerResponse.getSuccess("success",result);
    }

    @PostMapping(value = "/add")
    @ResponseBody
    @ApiOperation("新增版本")
    public ServerResponse add(@RequestBody CashVersionDto cashVersion, BindingResult result) {
        if(result.hasErrors()) {
            return ServerResponse.getErrorMessage(result.getAllErrors().toString());
        }
        if(StringUtils.isBlank(cashVersion.getName())) {
            cashVersion.setName(DEFAULT_APK_NAME);
        }
        Boolean addRes = cashVersionService.add(cashVersion);
        if(!addRes) {
            return ServerResponse.getErrorMessage("新增版本失败");
        }
        return ServerResponse.getSuccess("新增版本成功");
    }

    @PostMapping(value = "/update")
    @ResponseBody
    @ApiOperation("修改版本信息")
    public ServerResponse update(@RequestBody UpdateCashVersionDto cashVersion,BindingResult result) {
        if(result.hasErrors()) {
            return ServerResponse.getErrorMessage(result.getAllErrors().toString());
        }
        if(StringUtils.isBlank(cashVersion.getName())) {
            cashVersion.setName(DEFAULT_APK_NAME);
        }
        Boolean updateRes = cashVersionService.update(cashVersion);
        if(updateRes) {
            return ServerResponse.getSuccess("修改版本成功");
        }
        return ServerResponse.getSuccess("修改版本失败");
    }

    @GetMapping(value = "/detail")
    @ResponseBody
    @ApiOperation("版本详情")
    public ServerResponse detail(@RequestParam(value = "id") Integer id) {
        CashVersion cashVersion = cashVersionService.detail(id);
        return ServerResponse.getSuccess("success",cashVersion);
    }

    @PostMapping(value = "/updateStatus")
    @ResponseBody
    @ApiOperation("修改版本状态")
    public ServerResponse updateStatus(@RequestParam(value = "id") Integer id,
                                       @RequestParam(value = "status") Integer status) {
        Boolean updateRes = cashVersionService.updateStatus(id,status);
        if(updateRes) {
            return ServerResponse.getSuccess("修改状态成功");
        }
        return ServerResponse.getErrorMessage("修改状态失败");
    }

    @PostMapping(value = "/delete")
    @ResponseBody
    @ApiOperation("删除版本")
    public ServerResponse delete(@RequestParam(value = "id") Integer id) {
        Boolean delRes = cashVersionService.delete(id);
        if(delRes) {
            return ServerResponse.getSuccess("删除成功");
        }
        return ServerResponse.getErrorMessage("删除失败");
    }

    @PostMapping(value = "/upload")
    @ResponseBody
    @ApiOperation("版本上传")
    public ServerResponse uploadApkFile(@RequestPart("file") MultipartFile file) throws IOException {
        String name = file.getOriginalFilename().substring(0,file.getOriginalFilename().lastIndexOf("."));
        String filenameExtension = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."), file.getOriginalFilename().length());
        if(!APK_STRING.equals(filenameExtension)) {
            return ServerResponse.getErrorMessage("文件类型错误");
        }
        InputStream content = file.getInputStream();
        String fileAddr = qiniuService.upload(name,filenameExtension,content);
        Map<String,String> result = new HashMap<>(4);
        result.put("fileAddress",fileAddr);
        return ServerResponse.getSuccess("success",result);
    }
}
